<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>学生成绩修改页面</title>
    <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
    <link rel="stylesheet" href="/manageSystem/layui/css/layui.css">
    <!--    <link rel="stylesheet" href="layui-v2.6.8/layui/css/layui.css">-->
    <link rel="stylesheet" href="/manageSystem/css/common.css">


    <link rel="stylesheet" href="/manageSystem/layui/extend/dtree.css">
    <link rel="stylesheet" href="/manageSystem/layui/extend/font/dtreefont.css">
    <link rel="stylesheet" href="/manageSystem/layui/extend/formSelects-v4.css">
    <style>
        .layui-form{
            /*margin-right: 20px;*/
            margin: 40px 40px 40px 10px;
        }
        .layui-form-label{
            width: 150px;
            padding-left: 0;
            padding-right: 0;
        }
        .layui-input-block{
            margin-left: 170px;
        }
    </style>
</head>
<body>
<!--    <h1>学生成绩修改页面</h1>-->
    <form class="layui-form" action="">
        <input type="hidden" name="_method" value="PUT">
        <!--学号-->
        <div class="layui-form-item">
            <label for="studentId" class="layui-form-label">学号</label>
            <div class="layui-input-block">
                <input type="text" id="studentId" name="studentId" required  lay-verify="required" placeholder="学号" autocomplete="off" class="layui-input" disabled style="border-width: 0">
            </div>
        </div>
        <!--姓名-->
        <div class="layui-form-item">
            <label for="studentName" class="layui-form-label">姓名</label>
            <div class="layui-input-block">
                <input type="text" id="studentName" name="studentName" required  lay-verify="required" placeholder="姓名" autocomplete="off" class="layui-input" disabled style="border-width: 0">
            </div>
        </div>
        <!--班级-->
        <div class="layui-form-item">
            <label for="className" class="layui-form-label">班级</label>
            <div class="layui-input-block">
                <input type="text" id="className" name="className" required  lay-verify="required" placeholder="班级" autocomplete="off" class="layui-input" disabled style="border-width: 0">
            </div>
        </div>
        <!--科目1-->
        <div class="layui-form-item">
            <label for="subjectsScore1" id="subjectsScore1-label" class="layui-form-label"></label>
            <div class="layui-input-block">
                <input type="text" id="subjectsScore1" name="subjectsScore1" required  lay-verify="required" placeholder="分数" autocomplete="off" class="layui-input">
            </div>
        </div>
        <!--科目2-->
        <div class="layui-form-item">
            <label for="subjectsScore2" id="subjectsScore2-label" class="layui-form-label"></label>
            <div class="layui-input-block">
                <input type="text" id="subjectsScore2" name="subjectsScore2" required  lay-verify="required" placeholder="分数" autocomplete="off" class="layui-input">
            </div>
        </div>
        <!--科目3-->
        <div class="layui-form-item">
            <label for="subjectsScore3" id="subjectsScore3-label" class="layui-form-label"></label>
            <div class="layui-input-block">
                <input type="text" id="subjectsScore3" name="subjectsScore3" required  lay-verify="required" placeholder="分数" autocomplete="off" class="layui-input">
            </div>
        </div>
        <!--科目4-->
        <div class="layui-form-item">
            <label for="subjectsScore4" id="subjectsScore4-label" class="layui-form-label"></label>
            <div class="layui-input-block">
                <input type="text" id="subjectsScore4" name="subjectsScore4" required  lay-verify="required" placeholder="分数" autocomplete="off" class="layui-input">
            </div>
        </div>
        <!--科目5-->
        <div class="layui-form-item">
            <label for="subjectsScore5" id="subjectsScore5-label" class="layui-form-label"></label>
            <div class="layui-input-block">
                <input type="text" id="subjectsScore5" name="subjectsScore5" required  lay-verify="required" placeholder="分数" autocomplete="off" class="layui-input">
            </div>
        </div>
        <!--科目6-->
        <div class="layui-form-item">
            <label for="subjectsScore6" id="subjectsScore6-label" class="layui-form-label"></label>
            <div class="layui-input-block">
                <input type="text" id="subjectsScore6" name="subjectsScore6" required  lay-verify="required" placeholder="分数" autocomplete="off" class="layui-input">
            </div>
        </div>
        <!--科目7-->
        <div class="layui-form-item">
            <label for="subjectsScore7" id="subjectsScore7-label" class="layui-form-label"></label>
            <div class="layui-input-block">
                <input type="text" id="subjectsScore7" name="subjectsScore7" required  lay-verify="required" placeholder="分数" autocomplete="off" class="layui-input">
            </div>
        </div>
        <!--科目8-->
        <div class="layui-form-item" id="subjects8-state">
            <label for="subjectsScore8" id="subjectsScore8-label" class="layui-form-label"></label>
            <div class="layui-input-block">
                <input type="text" id="subjectsScore8" name="subjectsScore8" required  lay-verify="required" placeholder="分数" autocomplete="off" class="layui-input">
            </div>
        </div>
        <!--按钮-->
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
            </div>
        </div>
    </form>

    <script src="/js/jquery.js"></script>
    <script src="/manageSystem/layui/layui.js"></script>

    <script src="/js/myWebConfig.js"></script>
    <script>
        console.log("==================== 修改学生页面： ====================")
        // 获取学生信息对象。
        let studentInfoData = JSON.parse(window.sessionStorage.getItem("studentScoreInfo"))
        console.log("数据：")
        console.log(studentInfoData)

        $.ajaxSettings.async = true;   //恢复异步执行
    </script>
    <!--页面加载完成之后，进行数据请求-->
    <script>
        let subject = JSON.parse(window.sessionStorage.getItem("subjectNames"))

        $(function () {
            // 进行科目动态展示。
            console.log("当前回传班级科目名称：")
            console.log(JSON.parse(window.sessionStorage.getItem("subjectNames")))

            // 科目动态展示。
            // console.log("第十个元素：")
            // console.log(subject[10])
            if (subject[10] == undefined) {
                $("#subjectsScore1-label").text(subject[3])
                $("#subjectsScore2-label").text(subject[4])
                $("#subjectsScore3-label").text(subject[5])
                $("#subjectsScore4-label").text(subject[6])
                $("#subjectsScore5-label").text(subject[7])
                $("#subjectsScore6-label").text(subject[8])
                $("#subjectsScore7-label").text(subject[9])
                $("#subjectsScore8-label").val("")
                $("#subjectsScore8").val(0)

                $("#subjects8-state").hide()
            } else {
                $("#subjectsScore1-label").text(subject[3])
                $("#subjectsScore2-label").text(subject[4])
                $("#subjectsScore3-label").text(subject[5])
                $("#subjectsScore4-label").text(subject[6])
                $("#subjectsScore5-label").text(subject[7])
                $("#subjectsScore6-label").text(subject[8])
                $("#subjectsScore7-label").text(subject[9])
                $("#subjectsScore8-label").text(subject[10])

                $("#subjects8-state").show()
            }


            // 获取请求头信息。
            window.onload = function () {
                $("#studentId").val(studentInfoData.studentId)             // 学号
                $("#studentName").val(studentInfoData.studentName)         // 姓名
                $("#className").val(studentInfoData.className)             // 班级
                $("#subjectsScore1").val(studentInfoData.subjectsScore1)   // 科目1
                $("#subjectsScore2").val(studentInfoData.subjectsScore2)   // 科目2
                $("#subjectsScore3").val(studentInfoData.subjectsScore3)   // 科目3
                $("#subjectsScore4").val(studentInfoData.subjectsScore4)   // 科目4
                $("#subjectsScore5").val(studentInfoData.subjectsScore5)   // 科目5
                $("#subjectsScore6").val(studentInfoData.subjectsScore6)   // 科目6
                $("#subjectsScore7").val(studentInfoData.subjectsScore7)   // 科目7
                $("#subjectsScore8").val(studentInfoData.subjectsScore8 =="" ? 0: studentInfoData.subjectsScore8)   // 科目8
            }
        });
        function subjectsScoreExec(score) {
            // 分数校验。
            var subjectsScoreExec = /^(\d{1,3}|(\d{1,3}\.\d{0,2}))$/;
            return subjectsScoreExec.test(score);
        }
    </script>
    <script>
        layui.use('form', function() {
            var form = layui.form;

            //监听提交
            form.on('submit(formDemo)', function (data) {
                console.log("数据提交")
                console.log(data)
                console.log("学号：" + data.field.studentId)

                // js 进行数字校验。校验必须为数字。
                // var demo = /(\d{1,3})|(\d{1,3}\.[0-9][0-9])/;
                // 分数校验。
                // var subjectsScoreExec = /^(\d{1,3}|(\d{1,3}\.\d{0,2}))$/;
                // demo.test("123.00");

                if (!subjectsScoreExec(data.field.subjectsScore1)) {
                    parent.layer.msg("《java设计模式》的分数格式不正确，\n格式如：100.00。| 100", {time: 3000,icon: 5});
                    return false;
                }
                if (!subjectsScoreExec(data.field.subjectsScore2)) {
                    parent.layer.msg("《小灰漫画算法》的分数格式不正确，\n格式如：100.00。| 100", {time: 3000,icon: 5});
                    return false;
                }
                if (!subjectsScoreExec(data.field.subjectsScore3)){
                    parent.layer.msg("《深入了解虚拟机》的分数格式不正确，\n格式如：100.00。| 100", {time: 3000,icon: 5});
                    return false;
                };
                if (!subjectsScoreExec(data.field.subjectsScore4)){
                    parent.layer.msg("《java核心技术卷1》的分数格式不正确，\n格式如：100.00。| 100", {time: 3000,icon: 5});
                    return false;
                };
                if (!subjectsScoreExec(data.field.subjectsScore5)){
                    parent.layer.msg("《java设计模式》的分数格式不正确，\n格式如：100.00。| 100", {time: 3000,icon: 5});
                    return false;
                };
                if (!subjectsScoreExec(data.field.subjectsScore6)){
                    parent.layer.msg("《java编程思想》的分数格式不正确，\n格式如：100.00。| 100", {time: 3000,icon: 5});
                    return false;
                };
                if (!subjectsScoreExec(data.field.subjectsScore7)){
                    parent.layer.msg("《java设计模式》的分数格式不正确，\n格式如：100.00。| 100", {time: 3000,icon: 5});
                    return false;
                };
                // console.log( "验证：" + subjectsScoreExec.test("123"))
                // console.log( "验证：" + subjectsScoreExec.test("123.00"))
                // console.log( "验证：" + subjectsScoreExec.test("12300"))
                // console.log( "验证：" + subjectsScoreExec.test("123000"))
                // console.log( "验证：" + subjectsScoreExec.test("1230000"))
                // console.log( "验证：" + subjectsScoreExec.test("1230"))


                console.log("添加学生的信息：")
                console.log(data.field)
                let subjectList = []
                subjectList[0] = data.field.subjectsScore1
                subjectList[1] = data.field.subjectsScore2
                subjectList[2] = data.field.subjectsScore3
                subjectList[3] = data.field.subjectsScore4
                subjectList[4] = data.field.subjectsScore5
                subjectList[5] = data.field.subjectsScore6
                subjectList[6] = data.field.subjectsScore7
                subjectList[7] = data.field.subjectsScore8

                data.field = {
                    studentId: data.field.studentId
                    ,studentName: data.field.studentName
                    ,className: data.field.className
                    ,subjectsScore: subjectList
                }

                console.log(data.field)



                $.ajax({
                    // 出bug部分。
                    // url: webUrl+studentsDataPrefix+"/studentScoreInfo/"+window.sessionStorage.getItem("studentScoreInfo")+"/"+data.field.studentId,    // 修改学生成绩。
                    url: webUrl+studentsDataPrefix+"/studentScoreInfo/"+window.sessionStorage.getItem("classId")+"/"+data.field.studentId,    // 修改学生成绩。
                    type: 'PUT',
                    contentType: "application/json",//设置请求参数类型为json字符串
                    dataType: "json",
                    data: JSON.stringify(data.field),
                    success: function( response ) {
                        console.log("回传的数据")
                        console.log(response)
                        if (response.msg === "成功"){
                            parent.layer.msg("修改成功", {icon: 6});
                            // 关闭当前打开的页面。
                            parent.layer.close(2); //再执行关闭
                        }else {
                            parent.layer.msg("修改失败", {icon: 5});
                        }
                    }
                });
                return false;
            });
        });
    </script>
</body>
</html>